<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class InfoController extends Controller
{
    public function getinfo () {
        $skip = $_GET['skip'];//每次获取的数据量
        $section = $_GET['section'];//模块名
        switch ($section) {
            case 'index':
               $resourse = DB::table('dynamic')->select('id','d_id','d_jur','d_type','d_equi','d_time','d_zf','d_pl','d_like','d_posting')->skip($skip)->take(12)->get();


               foreach ($resourse as $v) {
                 $v->username = DB::table('user')->where('id',$v->d_id)->value('u_name');
                 $v->content = DB::table('dycont')->where('dy_id',$v->id)->value('dy_cont');
                 $v->userimg = DB::table('user')->where('id',$v->d_id)->value('u_img');
               }
               //return var_dump($resourse);
              // return var_dump($value);
               return response()->json($resourse);



            break;
        }
        return response()->json($v);
    }

    public function index ()
    {
        $resource = DB::table('dynamic')->skip(0)->take(12)->get();

        foreach ($resource as $v) {
            $v->username = DB::table('user')->where('id',$v->d_id)->value('u_name');
            $v->content = DB::table('dycont')->where('dy_id',$v->id)->value('dy_cont');
            $v->userimg = DB::table('user')->where('id',$v->d_id)->value('u_img');
        }

        //return var_dump($resource);
        $list = $this->API();
        foreach ($list as $k => $v){
            if ($k < 10){
                $vv[] = $v;
            }
        }

        $result = DB::table('music')->get();
        $link = DB::table('link')->get();

        return view ('home/index',['info'=>$resource,'result'=>$result, 'vv' => $vv, 'link'=>$link]);
    }

    public function count ()
    {
        $count = DB::table('dynamic')->count();
        return $count;
    }
    public  function people ()
    {
        $resource = DB::table('dynamic')->skip(0)->take(12)->get();

        foreach ($resource as $v) {
            $v->username = DB::table('user')->where('id',$v->d_id)->value('u_name');
            $v->content = DB::table('dycont')->where('dy_id',$v->id)->value('dy_cont');
            $v->userimg = DB::table('user')->where('id',$v->d_id)->value('u_img');
            //遍历获取转发微博
            if($v->d_type ==2){
                $resz = DB::table('uzf')->where('ro_id',$v->id)->value('ro_rid');
                $zcont = DB::table('dynamic')->where('id',$resz)->first();
                $zcont->zfcont = DB::table('dycont')->where('dy_id',$zcont->id)->value('dy_cont');
             $v->zfobj = $zcont;

            }

        }

        //ip地址模块

        $host = "https://dm-81.data.aliyun.com";
        $path = "/rest/160601/ip/getIpInfo.json";
        $method = "GET";
        $appcode = "7499e140e4cb45e981f27a5d50d8e276";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "ip=211.161.196.173";
        $bodys = "";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
     $res = json_decode(curl_exec($curl))->data;
        //return var_dump($res);

        $list = $this->API();
        foreach ($list as $k => $v){
            if ($k < 10){
                $vv[] = $v;
            }
        }

        $result = DB::table('music')->get();
        $link = DB::table('link')->get();



        return view('home/content/newpotic',['myinfo'=>$resource,'result'=>$result,'ipinfo'=>$res,'link' => $link,'vv' => $vv]);

    }
//获取评论信息
    public function plinfo(){
        $id = $_GET['plid'];
        $resource = DB::table('dycomment')->where('dc_id',$id)->get();
        foreach($resource as $v) {
            $v->content = DB::table('plcont')->where('pl_id',$v->id)->value('pl_cont');
            $v->conid = DB::table('plcont')->where('pl_id',$v->id)->value('id');
            $v->plname = DB::table('user')->where('id',$v->dc_pid)->value('u_name');
            $v->pimg = DB::table('user')->where('id',$v->dc_pid)->value('u_img');
        }
        return response()->json($resource);
    }

    //获取关注信息
    public function makegz(Request $request){
        $user = $_GET['userid'];
        if($user == session('userid')){
            return 3;
        }else{
            $resou = DB::table('attention')->where('at_uid',session('userid'))->where('at_gid',$user)->first();
            // return var_dump($resou);
            if(empty($resou)){
                return 0;
            } else{
                return 1;
            }
        }


    }

    private function API()
    {
        $host = "https://ali-qqmusic.showapi.com";
        $path = "/top";
        $method = "GET";
        $appcode = "6794ab5fe6784970a2c539d301ea1dac";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "topid=5";
        $bodys = "";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        // curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        // curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $data = curl_exec($curl);
        $jsonObj = json_decode($data);
        $obj = $jsonObj->showapi_res_body->pagebean->songlist;
//        echo '<pre>';
//        print_r($obj);
//        echo '</pre>';
        return $obj;
    }


   //获取收藏页面信息
   public function soucan (Request $request){
     $result = DB::table('msc')->where('uid',session('userid'))->get();

     foreach ($result as $k=>$v){
       $v->obj = DB::table('dynamic')->where('id',$v->dy_id)->get();
       $v->obj->content = DB::table('dycont')->where('dy_id',$v->dy_id)->value('dy_cont');
       $v->userimg = DB::table('user')->where('id',session('userid'))->value('u_img');
         $v->username = DB::table('user')->where('id',session('userid'))->value('u_name');
     }

     //return var_dump($result);
     return view('home/content/mysci',['result'=>$result]);
   }





}



